package com.nvn.washiodriver.network.ws;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.nvn.washiodriver.Objects.Schedule;
import com.nvn.washiodriver.Objects.ScheduleItem;
import com.nvn.washiodriver.network.NetworkUtils;
import com.nvn.washiodriver.network.WebServiceCommunicatorListener;
import com.nvn.washiodriver.utils.Statics;

@SuppressLint("SimpleDateFormat")
public class GetScheduleListWSControl extends BaseWSControl {
	private String userId;

	public GetScheduleListWSControl(Context context,
			WebServiceCommunicatorListener listener, String userId) {
		super(context, listener, WebServiceFlag.GET_SCHEDULE);
		this.userId = userId;

	}

	@Override
	protected Object doInBackground(Void... params) {
		// Get local database

		String url = Statics.WASHIO_WS_URL + "driver/schedule?userId=" + userId;

		try {
			String respone = NetworkUtils.doRequest(url);
			try {
				JSONObject job = new JSONObject(respone);
				return getScheduleFromJSONObject(job);

			} catch (JSONException e) {
				mError = e.getMessage();
			}

		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		}
		return null;
	}

	public Schedule getScheduleFromJSONObject(
			JSONObject job) throws JSONException {
		ArrayList<ScheduleItem> thisWeeks = new ArrayList<ScheduleItem>();
		ArrayList<ScheduleItem> nextWeeks = new ArrayList<ScheduleItem>();
		Schedule schedule = new Schedule();
		
		JSONObject jUser = Statics.getJSONObject(job, "user");
		schedule.setName(Statics.getString(jUser, "firstName") + " " + Statics.getString(jUser, "lastName"));
		schedule.setProfileUrl(Statics.getString(jUser, "profileUrl"));
		
		JSONObject shifts = Statics.getJSONObject(job, "shifts");
		
		JSONArray jThisWeek = Statics.getJSONArray(shifts, "thisWeek");
		for (int i = 0; i < jThisWeek.length(); i++) {
			JSONObject item = jThisWeek.getJSONObject(i);
			ScheduleItem sItem = new ScheduleItem();
			sItem.setAfternoon(Statics.getString(item, "afternoon"));
			sItem.setArea(Statics.getString(item, "area"));
			sItem.setDay(Statics.getString(item, "day"));
			sItem.setMorning(Statics.getString(item, "morning"));

			thisWeeks.add(sItem);

		}
		
		JSONArray jNextWeek = Statics.getJSONArray(shifts, "nextWeek");
		for (int i = 0; i < jNextWeek.length(); i++) {
			JSONObject item = jNextWeek.getJSONObject(i);
			ScheduleItem sItem = new ScheduleItem();
			sItem.setAfternoon(Statics.getString(item, "afternoon"));
			sItem.setArea(Statics.getString(item, "area"));
			sItem.setDay(Statics.getString(item, "day"));
			sItem.setMorning(Statics.getString(item, "morning"));
			
			nextWeeks.add(sItem);
			
		}
		
		schedule.setNextWeeks(nextWeeks);
		schedule.setThisWeeks(thisWeeks);
		
		return schedule;
	}
}
